class HotelTicketController < ApplicationController

  require 'will_paginate/array'
  require 'uri'

  def index
    check_token 
    @token = session["token"] 
    search = session[:hotel_ticket_search] 
    uri = URI('http://api.master18.tiket.com/search/hotel?')
    
    @sort = params[:sort]
    q = params[:q]
    minprice = params[:minprice]
    maxprice = params[:maxprice]
    maxstar = params[:maxstar] 
    minstar = params[:minstar] 
    if q
      startdate = params[:startdate]
      enddate = params[:enddate]
      adult = params[:adult]
      child = params[:child]
      night = params[:night]
      minprice = minprice.to_i * 1000
      maxprice = maxprice.to_i * 1000
      params = { q: q, startdate: startdate, currency: "USD", enddate: enddate, adult: adult, child: child, night: night ,:token => session[:token], :minprice => minprice, :maxprice => maxprice, minstar: minstar, maxstar: maxstar, :output => 'json' }
    elsif minprice or minstar
      minprice = minprice.to_i.to_i * 1000
      maxprice = maxprice.to_i * 1000
      params = { q: 'bali', adult: 1, child: 0, night: 1, sort: "popular", currency: "USD" ,:token => session[:token], :minprice => minprice, :maxprice => maxprice, minstar: minstar, maxstar: maxstar, :output => 'json' }
    elsif !@sort.blank?    
      params = { q: 'bali', adult: 1, child: 0, night: 1, sort: @sort.to_s, currency: "USD" ,:token => session[:token], :output => 'json' }
      @cek = 'bener masuk'
    else
      params = { q: 'bali', adult: 1, child: 0, night: 1, sort: "popular", currency: "USD" ,:token => session[:token], :output => 'json' }
    end

    @results = hotel_results(uri, params)

    respond_to do |format|
      format.html
      format.js {render layout: false}
      format.json { render json: @results }
    end
  end

  def autocomplete
    check_token 
    q = params[:term]
    uri = URI('http://api.master18.tiket.com/search/autocomplete/hotel?q='+q+'&token='+session[:token]+'&output=json')
    res = Net::HTTP.get_response(uri)
    @result = JSON.parse(res.body)["results"]["result"]
    respond_to do |format|
      format.json { render json: @result }
    end
  end

  def hotel_search
    check_token
    search = params[:search]
    session[:hotel_ticket_search] = params[:search]
    @uri = URI('http://api.master18.tiket.com/search/hotel?')
    params = { q: search[:q], startdate: search[:startdate], enddate: search[:enddate], adult: search[:adult], child: search[:child], night: search[:night], currency: "USD" ,:token => session[:token], :output => 'json' }
    @uri.query = URI.encode_www_form(params)
    res = Net::HTTP.get_response(@uri )
    @json = JSON.parse(res.body)
    @results = @json["results"]["result"].paginate(per_page: 10, page: params[:page] )

    respond_to do |format|
      format.js {render layout: false}
      format.json { render json: @json }
    end
  end

  def hotel_ticket_detail
    startdate = DateTime.strptime(params[:startdate].gsub('-','/'), "%Y/%m/%d")
    enddate = DateTime.strptime(params[:enddate].gsub('-','/'), "%Y/%m/%d")
    adult = params[:adult]
    child = params[:child]
    @night = (enddate - startdate).to_i
    uid = params[:uid]
    @uri = URI('http://api.master18.tiket.com/hotel/'+params[:name]+'?')
    params = { startdate: startdate, currency: "USD", enddate: enddate, adult: adult, child: child, night: @night ,:token => session[:token], uid: uid , :output => 'json' }
    @uri.query = URI.encode_www_form(params)
    @results = hotel_detail_results(@uri, params)

    respond_to do |format|
      format.html
      format.js {render layout: false}
      format.json { render json: @results }
    end
  end
  
  def show_room_facility
    @facilities = params[:room_facility]
    @facilities.to_a
    
    respond_to do |format|
      format.js {render layout: false}
    end
  end

  def add_room
    startdate = params[:startdate]
    enddate = params[:enddate]
    night = params[:night]
    room = params[:room]
    adult = params[:adult]
    child = params[:child]
    minstar = params[:minstar]
    maxstar = params[:maxstar]
    minprice = params[:minprice]
    maxprice = params[:maxprice]
    hotelname = params[:hotelname]
    room_id = params[:room_id]
    hasPromo = params[:hasPromo]
    @uri = URI('http://api.master18.tiket.com/order/add/hotel?')
    params = { startdate: startdate, enddate: enddate, adult: adult, child: child, night: night, :room => room,  
      minstar: minstar,  maxstar: maxstar, minprice: minprice, maxprice: maxprice, hotelname: hotelname, room_id: room_id, hasPromo: hasPromo, :token => session[:token], :output => "json" }
    @uri.query = URI.encode_www_form(params)
    res = Net::HTTP.get_response(@uri )
    @json = JSON.parse(res.body)
     
      respond_to do |format|
        format.js {render layout: false}
      end
  end

  def hotel_order
    # list hotel order
    @uri = URI('http://api.master18.tiket.com/order?')
    params = {:token => session[:token], :output => "json" }
    @uri.query = URI.encode_www_form(params)
    res = Net::HTTP.get_response(@uri )
    @results = JSON.parse(res.body)

    # list hotel reference
    ref = URI('http://api.master18.tiket.com/search/hotel?')
    params = {:uid => 'city:'+params[:cid].to_s ,:token => session[:token], :output => "json" }
    ref.query = URI.encode_www_form(params)
    res = Net::HTTP.get_response(ref )
    @references = JSON.parse(res.body)

    respond_to do |format|
      format.html
    end
  end

  def del_hotel_order
    @uri = URI('http://api.master18.tiket.com/order/delete_order?')
    params = {:token => session[:token], :output => "json" }
    @uri.query = URI.encode_www_form(params)
    res = Net::HTTP.get_response(@uri )
    @results = JSON.parse(res.body)

    respond_to do |format|
      format.js 
    end
  end

  def hotel_customer_checkout
    session[:check] = ''
    #  order check out
    @uri = URI('http://api.master18.tiket.com/order/checkout/'+params[:order_id].to_s+'/IDR?')
    params = {:token => session[:token], :output => "json" }
    @uri.query = URI.encode_www_form(params)
    res = Net::HTTP.get_response(@uri )
    @results = JSON.parse(res.body)

    session[:check] += "Checkout Page Request " if @results['diagnostic']['status'].to_i == 200

    # check out login
    @log = URI('http://api.master18.tiket.com/checkout/checkout_customer?')
    params = {:token => session[:token], :output => "json", salutation: 'Mr', firstName: 'bali', lastName: 'fantastico', emailAddress: 'info@balifantastico.com', phone: '+62878434343', output: 'json', saveContinue: 2 }
    @log.query = URI.encode_www_form(params)
    res = Net::HTTP.get_response(@log)
    @login = JSON.parse(res.body)

    session[:check] += "Login Request " if @login['diagnostic']['status'].to_i == 200

    respond_to do |format|
      format.html 
    end
  end

  def hotel_payment
    conSalutation = params[:conSalutation]
    conFirstName = params[:conFirstName]
    conLastName = params[:conLastName]
    conEmailAddress = params[:conEmailAddress]
    conPhone = params[:conPhone]
    detailId = params[:detailId]
    @payment = params[:payment]

    @uri = URI('http://api.master18.tiket.com/checkout/checkout_customer?')
    params = {:token => session[:token], :output => "json", salutation: 'Mr', firstName: 'bali', lastName: 'fantastico', emailAddress: 'info@balifantastico.com', phone: '%2B62878434343', conSalutation: conSalutation , conFirstName: conFirstName, conLastName: conLastName , conEmailAddress: 'dana@balifantastico.com', conPhone: '%2B62878434343'+conPhone.to_s , detailId: detailId , country: 'id'}
    @uri.query = URI.encode_www_form(params)
    res = Net::HTTP.get_response(@uri )
    @results = JSON.parse(res.body)

    session[:check] += "Checkout Customer " if @results['diagnostic']['status'].to_i == 200

    respond_to do |format|
      format.html 
    end
  end
end
